/**
 * Created by koudaiwang on 2017/7/3.
 */

var JsonModel = require('../config/jsonModel');
var XXXModel = require('../Dao/admindao');


module.exports = {
    // 检查管理员是否登录

    checkAdminLogin: function (req, res, next) {
        var model = new JsonModel.JsonModel();
        model.status = 0;
        var content = {};
        if (req.method === 'GET') {
            content = req.query;
        } else if (req.method === 'POST') {
            content = req.body;
        } else {
            model.msg = '请求方法不对!';
            model.content = {};
            return res.send(model);
        }
        content = !content ? {} : content;
        var deviceType = content.deviceType;
        var sessionID = content.sessionID;
        var username = content.username;

        try {
            if (!deviceType) {
                throw new Error('设备类型不能为空!');
            }
            if (!username) {
                throw new Error('用户名不能为空!');
            }
            if (!sessionID) {
                throw new Error('sessionID不能为空!');
            }
        } catch (e) {
            model.content = e.message;
            model.msg = e.message;
            return res.send(model);
        }
        var data = {};
        XXXModel.getAdminByName(username).then(function (result) {
            var user = result[0];
            if (!user) {
                model.content = {};
                model.status = 0;
                model.msg = '没有该用户!';
                return res.send(model);
            }
            if (username === user.username) {
                    var sessionIDDB = '';
                    if (deviceType.toLowerCase() === 'web') {
                        data['webLoginTime'] = new Date().getTime().toString();
                        sessionIDDB = user.webLogin;
                    } else if (deviceType.toLowerCase() === 'ios') {
                        data['iOSLoginTime'] = new Date().getTime().toString();
                        sessionIDDB = user.iOSLogin;
                    } else if (deviceType.toLowerCase() === 'android') {
                        data['androidLoginTime'] = new Date().getTime().toString();
                        sessionIDDB = user.androidLogin;
                    } else {
                        model.content = {};
                        model.msg = '设备类型不正确!';
                        return res.send(model);
                    }

                    if (sessionID === sessionIDDB) {
                        XXXModel.updateLoginStatus(user._id, data).then(function () {
                            next();
                        }).catch(function (error) {
                            model.content = {};
                            model.msg = error.message;
                            return res.send(model);
                        });
                    } else {
                        model.content = {};
                        model.msg = '登录状态不匹配！';
                        return res.send(model);
                    }
                } else  {
                model.status = 0;
                model.msg = '未登录!';
                model.content = {};
                return res.send(model);
            }
        });
    },

    // 检查用户是否登录

    checkUserLogin: function (req, res, next) {
        var model = new JsonModel.JsonModel();
        model.status = 0;
        var content = {};
        if (req.method === 'GET') {
            content = req.query;
        } else if (req.method === 'POST') {
            content = req.body;
        } else {
            model.msg = '请求方法不对!';
            model.content = {};
            return res.send(model);
        }
        content = !content ? {} : content;
        var deviceType = content.deviceType;
        var sessionID = content.sessionID;
        var username = content.username;

        try {
            if (!deviceType) {
                throw new Error('设备类型不能为空!');
            }
            if (!username) {
                throw new Error('用户名不能为空!');
            }
            if (!sessionID) {
                throw new Error('sessionID不能为空!');
            }
        } catch (e) {
            model.content = e.message;
            model.msg = e.message;
            return res.send(model);
        }
        var data = {};
        XXXModel.getUserByName(username).then(function (result) {
            if (result.count === 0) {
                model.content = {};
                model.status = 0;
                model.msg = '没有该用户!';
                return res.send(model);
            }
            var user = result[0];
            if (!user) {
                model.content = {};
                model.status = 0;
                model.msg = '没有该用户!';
                return res.send(model);
            }
            if (username === user.username) {
                    var sessionIDDB = '';
                    if (deviceType.toLowerCase() === 'web') {
                        data['webLoginTime'] = new Date().getTime().toString();
                        sessionIDDB = user.webLogin;
                    } else if (deviceType.toLowerCase() === 'ios') {
                        data['iOSLoginTime'] = new Date().getTime().toString();
                        sessionIDDB = user.iOSLogin;
                    } else if (deviceType.toLowerCase() === 'android') {
                        data['androidLoginTime'] = new Date().getTime().toString();
                        sessionIDDB = user.androidLogin;
                    } else {
                        model.content = {};
                        model.msg = '设备类型不正确!';
                        return res.send(model);
                    }

                    if (sessionID === sessionIDDB) {
                        XXXModel.updateLoginStatus(user._id, data).then(function () {
                            next();
                        }).catch(function (error) {
                            model.content = {};
                            model.msg = error.message;
                            return res.send(model);
                        });
                    } else {
                        model.content = {};
                        model.msg = '登录状态不匹配！';
                        return res.send(model);
                    }
                } else  {
                model.status = 0;
                model.msg = '未登录!';
                model.content = {};
                return res.send(model);
            }
        });
    }
}

